<template>
  <el-dialog
    title="调整分销员等级"
    :visible.sync="visible"
    :before-close="handleClose"
    width="25%"
  >
    <div style="color:#F56C6C;">
      {{
        val.distribute_lv_id
          ? `当前分销员等级：${val.distribute_name}`
          : "当前选中的分销员"
      }}
    </div>
    <br />
    <div>
      <el-form ref="form" :model="form">
        <el-form-item
          prop="selectVal"
          label="调整等级至"
          :rules="{ required: true, message: '请选择等级', trigger: 'blur' }"
        >
          <el-select
            v-model="form.selectVal"
            placeholder="请选择"
            size="small"
            style="margin-right:10px;"
          >
            <el-option
              v-for="item in options"
              :key="item.distribute_lv_id"
              :label="item.name"
              :value="item.distribute_lv_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSumbit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { EDITLV, DEFAULTBILI } from "../../../service";

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    val: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        selectVal: ""
      },
      options: []
    };
  },
  watch: {
    visible(v) {
      if (v) {
        this.form.selectVal = this.val.distribute_lv_id
          ? this.val.distribute_lv_id
          : "";
        this.default();
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit("outDialog");
    },
    async handleSumbit() {
      await this.$refs["form"].validate(valid => {
        if (valid) {
          EDITLV({
            distribute_lv_id: this.form.selectVal,
            member_ids: this.val.member_ids
          }).then(({ code, msg }) => {
            if (code === 200) {
              this.$emit("outDialog", true);
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        }
      });
    },
    async default() {
      await DEFAULTBILI().then(({ code, data }) => {
        if (code === 200) {
          this.options = data;
        }
      });
    }
  }
};
</script>
